/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-13 10:40:50
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-17 16:06:09
 * @FilePath: /GateServer/src/validator/resetpswvalidator.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "resetpswvalidator.h"
#include "../mysql/mysqlmgr.h"
#include "../redis/redismgr.h"
#include "validator.h"
              
namespace Yizhi {
    ResetPswValidtor::validateResult ResetPswValidtor::validate(const User& user) {
        int vc=vaildateVarifycode(user.getEmail(), user.getVarifycode());
        if(vc==-1){
            return VARIFY_CODE_INVALID;
        }
        else if(vc==0){
            return VARIFY_CODE_EXPIRED;
        }

        if(!checkUserExist(user.getUsername(),user.getEmail())){
            return USER_NOT_EXISTS;
        }

        return SUCCESS;
    }

    bool ResetPswValidtor::checkUserExist(std::string username,std::string email){
        auto res=MysqlMgr::GetInstance()->finduserbyemail(email);
        return res;
    }
    int ResetPswValidtor::vaildateVarifycode(const std::string &email,const std::string &code){
        std::string value;
        auto res=RedisMgr::GetInstance()->getvalue(email,value);

        if(res){
            return value==code;
        }
        return -1;
    }
}